package com.irs.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.irs.pojo.TbLotData;
import com.irs.pojo.TbLotSearch;
import com.irs.service.DeviceService;
import com.irs.util.ResultUtil;

import cmcc.iot.onenet.javasdk.api.datapoints.AddDatapointsApi;
import cmcc.iot.onenet.javasdk.api.datapoints.GetDatapointsListApi;
import cmcc.iot.onenet.javasdk.model.Data;
import cmcc.iot.onenet.javasdk.model.Datapoints;
import cmcc.iot.onenet.javasdk.response.BasicResponse;
import cmcc.iot.onenet.javasdk.response.datapoints.DatapointsList;

@Controller
@RequestMapping("device/")
public class DeviceController {

	@Autowired
	private DeviceService deviceService;

	private static final String key = "TDdR9rOFPZvxN1IhPSVK1qp==5k=";
	private static final String devid = "562549893";
	private static final String lotData0 = "湿度";
	private static final String lotData1 = "温度";

	// 跳转首页
	@RequestMapping("lotList")
	public String lotList() {
		return "page/user/userList";
	}

	// 添加数据
	@RequestMapping("addLot")
	@ResponseBody
	public ResultUtil addLot(TbLotData tbLotData) {
		try {
			List<Datapoints> list = new ArrayList<Datapoints>();
			List<Data> dl = new ArrayList<Data>();
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			String dateTime = sdf2.format(new Date());
			dl.add(new Data(dateTime, new Random().nextInt(100)));
			dl.add(new Data(dateTime, new Random().nextInt(100)));
			list.add(new Datapoints(lotData0, dl));
			list.add(new Datapoints(lotData1, dl));
			Map<String, List<Datapoints>> map = new HashMap<String, List<Datapoints>>();
			map.put("datastreams", list);
			AddDatapointsApi api = new AddDatapointsApi(map, null, null, devid, key);
			BasicResponse<Void> response = api.executeApi();
			System.out.println("errno:"+response.errno+" error:"+response.error);
			// TODO 解析入库
			
			
			deviceService.addData(tbLotData);
			return ResultUtil.ok();
		} catch (Exception e) {
			return new ResultUtil(502, "添加失败,请检查!");
		}
	}

	// 获取最新数据
	@RequestMapping("getLastData")
	@ResponseBody
	public ResultUtil getLastData() {
		try {
			String datastreamids = lotData0 + "," + lotData1;
			GetDatapointsListApi api = new GetDatapointsListApi(datastreamids, null, null, devid, null, null, null,
					null, null, null, null, key);
			BasicResponse<DatapointsList> response = api.executeApi();
			System.out.println(response.getJson());
			return ResultUtil.ok(response.getJson());
		} catch (Exception e) {
			return new ResultUtil(502, "添加失败,请检查!");
		}
	}

	// 数据分页
	@RequestMapping("/lotPage")
	@ResponseBody
	public ResultUtil getCarouseList(Integer page, Integer limit,TbLotSearch search) {
		ResultUtil carousels = deviceService.selTbLotData(page, limit,search);
		// TODO条件不好使
		return carousels;
	}

}
